// pages/personal/perinfo.js
var util = require('../../utils/util');
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    title: '请输入',
    errorMsg: true,
    hidden: true,
    type: 1,
    temp: '',
    inputText: '',
    array: ["男", "女"]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    util.sendRequest('WxGetUserInfo', {}, function (resp) {
      resp.result.tSex = (resp.result.sex == undefined || resp.result.sex == 0) ? 0 : (parseInt(resp.result.sex) - 1);
      that.setData({ userInfo: resp.result });
    }, '加载中...', function () {

    });
  },
  bindPickerChange: function (e) {
    var sex = e.detail.value;
    var userInfo = that.data.userInfo;
    userInfo.sex = sex;
    sex = parseInt(e.detail.value) + 1;
    util.sendRequest('WxUpdateUser', { sex: sex }, function (resp) {
      if (resp.code == 0) {
        userInfo.tSex = sex - 1;
        that.setData({
          userInfo: userInfo
        });
      }
    }, '修改中...', function () {

    });
  },
  bindDateChange: function (e) {
    var birthday = e.detail.value;
    var userInfo = that.data.userInfo;
    userInfo.birthday = birthday;
    util.sendRequest('WxUpdateUser', { birthday: birthday }, function (resp) {
      if (resp.code == 0) {
        that.setData({
          userInfo: userInfo
        });
      }
    }, '修改中...', function () {

    });

  },
  bindInputText: function (e) {
    const inputVal = e.detail.value;
    // 手机号
    if (this.data.type === 2) {
      if (!(/^\d{11}$/.test(inputVal))) {
        this.setData({
          errorMsg: false
        });
      } else {
        that.setData({
          temp: inputVal,
          errorMsg: true
        });
      }
    } else {
      that.setData({ temp: inputVal });
    }
  },
  editNick: function () {
    that.setData({ hidden: false, title: '请输入昵称', type: 1, inputText: this.data.userInfo.nickName });
  },
  editPhone: function () {
    that.setData({ hidden: false, title: '请输入手机号', type: 2, inputText: this.data.userInfo.phone });
  },
  editHeight: function () {
    that.setData({ hidden: false, title: '请输入身高', type: 3, inputText: '' });
  },
  confirm: function () {
    if (!that.data.temp) {
      that.setData({
        hidden: true
      });
      return;
    }
    if (that.data.type == 1) {
      var userInfo = that.data.userInfo;
      userInfo.nickName = that.data.temp;

      util.sendRequest('WxUpdateUser', { nickName: userInfo.nickName }, function (resp) {
        if (resp.code == 0) {
          that.setData({
            userInfo: userInfo
          });
        }
      }, '修改中...', function () {

      });

    } else if (that.data.type == 2) {
      var userInfo = that.data.userInfo;
      userInfo.phone = that.data.temp;
      util.sendRequest('WxUpdateUser', { phone: userInfo.phone }, function (resp) {
        if (resp.code == 0) {
          that.setData({
            userInfo: userInfo
          });
        }
      }, '修改中...', function () {

      });
    } else {
      var userInfo = that.data.userInfo;
      userInfo.height = that.data.temp;
      util.sendRequest('WxUpdateUser', { height: userInfo.height }, function (resp) {
        if (resp.code == 0) {
          that.setData({
            userInfo: userInfo
          });
        }
      }, '修改中...', function () {

      });
    }
    that.setData({
      hidden: true
    });
  },
  cancel: function () {
    that.setData({
      hidden: true
    });
  },
  bindAddHeadImg: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      success: function (res) {
        var filePath = res.tempFilePaths.join(',');
        util.uploadImage(filePath, 'headImg', function (obj) {

          util.sendRequest('WxUpdateUser', { headImg: obj.headImg }, function (resp) {
            if (resp.code == 0) {
              var userInfo = that.data.userInfo;
              userInfo.headImg = util.serviceUrl + obj.url;
              that.setData({
                userInfo: userInfo
              });
            }
          }, '修改中...', function () {

          });
        }, function (res) {
          that.setData({
            uploadLessonImg: '../../images/btn-upload.png',
            lessonImg: ''
          });
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return util.getShareMsg();
  }
})